define(['jquery', 'bootstrap', 'backend', 'table', 'form','vue'], function ($, undefined, Backend, Table, Form,Vue) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wanlshop/activity/index' + location.search,
                    add_url: 'wanlshop/activity/add',
                    edit_url: 'wanlshop/activity/edit',
                    del_url: 'wanlshop/activity/del',
                    multi_url: 'wanlshop/activity/multi',
                    import_url: 'wanlshop/activity/import',
                    table: 'wanlshop_activity',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'images', title: __('Goods_ids'), operate: false, formatter: Table.api.formatter.images},
                        {field: 'starttime', title: __('Starttime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'endtime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'state',title: __('Status'),searchList: {"created": __('未开始'),"doing": __('进行中'),"expired": __('已过期')},formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table,buttons: [
                                {
                                    name: 'edit',
                                    text: __('查看详情'),
                                    title: __('查看详情'),
                                    classname: 'btn btn-xs btn-warning btn-editone',
                                    icon: 'fa fa-eye',
                                }
                            ], events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            table.on('load-success.bs.table',function(data){
                $(".btn-editone").data("area", ["90%","80%"]);
                $(".btn-add").data("area", ["90%","80%"]);
                $(".btn-edit").data("area", ["90%","80%"]);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'wanlshop/activity/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'wanlshop/activity/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'wanlshop/activity/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            var vm = new Vue({
                el: '#app',
                data() {
                    return {
                        goods_ids: [],
                        goods : [],
                        goods_ids_input : '',
                        goods_sku : '',
                    }
                },
                methods: {
                    goodsLink: function () {
                        let that = this;
                        parent.Fast.api.open("wanlshop/goods/select", __("选择商品"), {
                            area: ["800px", "600px"],
                            callback: function (i) {
                                if(that.goods_ids.indexOf(i.url) === -1){
                                    that.goods_ids.push(i.url);
                                }
                                that.getGoods();
                            }
                        })
                    },
                    getGoods:function () {
                        let that = this;
                        Fast.api.ajax({
                            url : "wanlshop/goods/select",
                            type: "POST",
                            data:{
                                type: 'activity',
                                goods_ids: that.goods_ids
                            }
                        }, function(data,ret){
                            if(ret.code == 1){
                                that.goods = ret.data;
                                that.writeInput()
                            }
                            return false;
                        })
                    },
                    remove:function (index) {
                        let that = this;
                        var confirm = Layer.confirm('确定移除该活动商品?',
                            {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                            function (i) {
                                that.goods_ids.splice(index,1)
                                that.goods.splice(index,1)
                                Layer.close(confirm)
                                that.writeInput()
                            }
                        );
                    },
                    setSku:function (index,goods_id) {
                        let that = this;
                        parent.Fast.api.open("wanlshop/goods/setsku/ids/"+goods_id, __("设置活动商品"), {
                            area: ["800px", "600px"],
                            callback: function (data) {
                                that.goods[index].activity_sku = data.data;
                                that.writeInput()
                                console.log(that.goods);
                            }
                        })
                    },
                    writeInput(){
                        let that = this;
                        that.goods_ids_input = JSON.stringify(that.goods_ids);
                        that.goods_sku = JSON.stringify(that.goods);

                    }
                }
            })
            Controller.api.bindevent();
        },
        edit: function () {
            var vm = new Vue({
                el: '#app',
                data() {
                    return {
                        goods_ids: Config.goods_ids,
                        activity_id: Config.activity_id,
                        goods : [],
                        goods_ids_input : '',
                        goods_sku : '',
                    }
                },
                mounted(){
                    console.log(this.goods_ids);
                    this.getGoods();
                },
                methods: {
                    goodsLink: function () {
                        let that = this;
                        parent.Fast.api.open("wanlshop/goods/select", __("选择商品"), {
                            area: ["800px", "600px"],
                            callback: function (i) {
                                if(that.goods_ids.indexOf(i.url) === -1){
                                    that.goods_ids.push(i.url);
                                }
                                that.getGoods();
                            }
                        })
                    },
                    getGoods:function () {
                        let that = this;
                        Fast.api.ajax({
                            url : "wanlshop/goods/select",
                            type: "POST",
                            data:{
                                type: 'activity',
                                goods_ids: that.goods_ids
                            }
                        }, function(data,ret){
                            if(ret.code == 1){
                                that.goods = ret.data;
                                that.writeInput()
                            }
                            return false;
                        })
                    },
                    remove:function (index) {
                        let that = this;
                        var confirm = Layer.confirm('确定移除该活动商品?',
                            {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                            function (i) {
                                that.goods_ids.splice(index,1)
                                that.goods.splice(index,1)
                                Layer.close(confirm)
                                that.writeInput()
                            }
                        );
                    },
                    viewSku:function (index,goods_id) {
                        let that = this;
                        parent.Fast.api.open("wanlshop/goods/setsku/ids/" + goods_id +"?activity_id=" + that.activity_id, __("查看活动商品"), {
                            area: ["800px", "600px"],

                        })
                    },
                    writeInput(){
                        let that = this;
                        that.goods_ids_input = JSON.stringify(that.goods_ids);
                        that.goods_sku = JSON.stringify(that.goods);

                    }
                }
            })
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});